package TagRate.project.pages.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.web_bloks.content.client.ContentPage;
import com.web_bloks.http.client.JSONResponse;
import com.web_bloks.system.client.Logger;
import com.web_bloks.types.client.StateType;
import com.web_bloks.widget.client.StatusWidget;
import com.web_bloks.widget.client.StatusWidget.Status;

/**
 * Must be logged-in before using this page
 * 
 * @author Jean-Lou Dupont
 *
 */
public class ConfigPage extends Composite 
	implements ContentPage { 

//{{
	Timer timer = null;
	final int DEFAULT_TIMEOUT = 5000;
	
	VerticalPanel verticalPanel=null;
	FormPanel formPanel = null;
	FlexTable flexTable = null;
	Label labelUsername = null;
	TextBox textBoxUsername = null;
	Label labelPassword = null;
	PasswordTextBox textBoxPassword = null;
	Button buttonSubmit = null;
	StatusWidget statusWidget = null;
	Label labelInstructions = null;
	HTML htmlFooter = null;
	
	public ConfigPage() {

		verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("100%", "100%");

		formPanel = new FormPanel();
		verticalPanel.add(formPanel);
		formPanel.setWidth("75%");
		verticalPanel.setCellWidth(formPanel, "75%");
		verticalPanel.setCellHorizontalAlignment(formPanel, HasHorizontalAlignment.ALIGN_CENTER);

		flexTable = new FlexTable();
		formPanel.setWidget(flexTable);
		flexTable.setSize("100%", "100%");

		labelUsername = new Label();
		flexTable.setWidget(1, 1, labelUsername);

		textBoxUsername = new TextBox();
		flexTable.setWidget(1, 2, textBoxUsername);
		textBoxUsername.setName("username");
		textBoxUsername.setTabIndex(1);
		textBoxUsername.setTextAlignment(TextBoxBase.ALIGN_LEFT);
		textBoxUsername.setVisibleLength(24);
		textBoxUsername.setMaxLength(24);
		textBoxUsername.setWidth("100%");

		labelPassword = new Label();
		flexTable.setWidget(2, 1, labelPassword);

		textBoxPassword = new PasswordTextBox();
		flexTable.setWidget(2, 2, textBoxPassword);
		textBoxPassword.setName("password");
		textBoxPassword.setTabIndex(2);
		textBoxPassword.setTextAlignment(TextBoxBase.ALIGN_LEFT);
		textBoxPassword.setVisibleLength(32);
		textBoxPassword.setMaxLength(24);

		buttonSubmit = new Button();
		flexTable.setWidget(3, 1, buttonSubmit);
		buttonSubmit.setTabIndex(3);

		statusWidget = new StatusWidget();
		flexTable.setWidget(2, 0, statusWidget);
		flexTable.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER);
		statusWidget.setStatus(StateType.NA);

		labelInstructions = new Label("New Label");
		flexTable.setWidget(0, 0, labelInstructions);
		labelInstructions.setWidth("100%");
		flexTable.getCellFormatter().setWidth(0, 0, "100%");
		flexTable.getCellFormatter().setHeight(0, 0, "75");
		flexTable.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getFlexCellFormatter().setColSpan(0, 0, 3);
		flexTable.getCellFormatter().setHorizontalAlignment(3, 2, HasHorizontalAlignment.ALIGN_CENTER);

		htmlFooter = new HTML();
		flexTable.setWidget(4, 0, htmlFooter);
		htmlFooter.setSize("100%", "75");
		flexTable.getCellFormatter().setWidth(4, 0, "100%");
		flexTable.getCellFormatter().setHeight(4, 0, "50");
		htmlFooter.setVisible(false);
		flexTable.getCellFormatter().setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getFlexCellFormatter().setColSpan(4, 0, 3);
		
		setup();
	}

	/*
	 * @see TagRate.project.client.ContentPage#getDisplayWidget()
	 */
	public Widget getDisplayWidget() {

		return this;
	}
	/*
	 * @see TagRate.project.client.ContentPage#getPageName()
	 */
	public String getPageName() {
		return "Config";
	}

	/*
	 * @see TagRate.project.client.ContentPage#requiresLogin()
	 */
	public boolean requiresLogin() {
		return true;
	}
	
	protected void setup() {
		
		setupTimer();
		
		PageMessages pm = GWT.create(PageMessages.class);
		
		labelInstructions.setText(pm.config_instructions());
		labelUsername.setText(pm.username());
		labelPassword.setText(pm.password());
		buttonSubmit.setText(pm.submit());
		formPanel.setAction("/user/delicious/");
		formPanel.setMethod( FormPanel.METHOD_POST );
		
		buttonSubmit.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				formPanel.submit();
				timer.schedule(DEFAULT_TIMEOUT);
			}
		});
		
		formPanel.addFormHandler( new FormHandler() {

			public void onSubmit(FormSubmitEvent event) {
				Logger.logInfo(this.getClass()+".formHandler.onSubmit");
				statusWidget.setStatus(StateType.PENDING);
			}

			public void onSubmitComplete(FormSubmitCompleteEvent event) {
				Logger.logInfo(this.getClass()+".formHandler.onSubmitComplete, event msg=" + event.getResults());
				String response = event.getResults();
	
				timer.cancel();
				
				boolean result = processResponse( response );
				
				if (!result) {
					statusWidget.setStatus(StateType.ERROR);
					displayFooter( false );
				} else {
					statusWidget.setStatus(StateType.OK);
					displayFooter( true );
				}
			}
			
		} );
	}
	/**
	 * 
	 */
	protected boolean processResponse(String response) {
		
		JSONResponse jr = JSONResponse.fromJSONString(response);
		
		if (!jr.contains("code"))
			return false;
		
		return jr.getString("code") == "200";
	}
	
	protected void setupTimer() {
	
		if (null!=timer)
			return;
		
		timer = new Timer() {
			public void run() {
				statusWidget.setStatus(StateType.ERROR);	
				displayFooter( false );
			}
		};
		
	}
	
	protected void displayFooter( boolean result ) {
		
		htmlFooter.setVisible(result);
		
		if (result) {
			PageMessages MSG = GWT.create(PageMessages.class);
			htmlFooter.setHTML( MSG.test_config() );
			
		}
	}

	public void setParams(String params) {
		// TODO Auto-generated method stub
		
	}
	
}//END
